package com.lh.common.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @description: 返回数据
 * @author: Wudp
 * @create: 2020-04-07 17:37
 **/
@Data
public class Result<T> {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应码")
    private int code;
    @ApiModelProperty(value = "响应信息")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param msg
     * @param data
     */
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result ok() {
        return new Result(0,"success",null);
    }

    public static Result ok(Object data) {
        if(data == null){
            return new Result(0,"success",null);
        }
        return new Result(0,"success",data);
    }

    public static Result ok(String msg,Object data) {
        if(data == null){
            return new Result(0,msg,"");
        }
        return new Result(0,msg,data);
    }

    public static Result error(int code, String msg) {
        return new Result(code,msg,null);
    }

    public static Result error(String msg) {
        return Result.error(Constant.RESULT.EX500,msg);
    }

    public static Result error(int code) {
        return Result.error(code,"操作失败");
    }

    public static Result error() {
        return Result.error("操作失败");
    }

}

